//
//  FloatingWindowView.m
//  HollyPhoneSdk
//
//  Created by hollycrm on 2023/3/3.
//

#import "FloatingWindowView.h"
#import "HollyPhoneManager.h"
#import "Utils.h"
#import "Common.h"

@interface FloatingWindowView(){
    UIView *floatView;
    UIImageView *floatImage;
    UIButton *floatBt;
    UILabel *floatLabel;
    NSBundle *bundle;
}

@end
@implementation FloatingWindowView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
        self->bundle = [Utils getSdkBundle];
    }
    return self;
}

-(void)refreshFloatView{
    //挂断
    if([HollyPhoneManager shareManager].phoneStatus.callStatus == 0){
        if (@available(iOS 13.0, *)) {
            UIImage *image = [UIImage imageNamed:@"hangup_red.png" inBundle:self->bundle withConfiguration:nil];
            [floatImage setImage:image];
            int second = [HollyPhoneManager shareManager].phoneStatus.callSeconds;
            floatLabel.text = [Utils getCallTimeStr:second];
        } else {
            // Fallback on earlier versions
        }
    }
    else{
        if (@available(iOS 13.0, *)) {
            UIImage *image = [UIImage imageNamed:@"phone_green.png" inBundle:self->bundle withConfiguration:nil];
            [floatImage setImage:image];
        } else {
            // Fallback on earlier versions
        }
        int second = [HollyPhoneManager shareManager].phoneStatus.callSeconds;
        floatLabel.text = [Utils getCallTimeStr:second];
    }
    [self setNeedsLayout];
}

-(void)floatViewClick{
    PhoneStatus *phone = [HollyPhoneManager shareManager].phoneStatus;
    [[[Common alloc] init] goToCallView:phone.callId with:phone.phoneNum isRestore:true];
}

-(void)setupViews{
    self->floatView = [[UIView alloc] initWithFrame:CGRectMake(8, 0, 70, 70)];
    self->floatView.layer.masksToBounds = YES;
    self->floatView.layer.cornerRadius = 16.0;
    self->floatView.layer.borderWidth = 1.0;
    [self->floatView setBackgroundColor:UIColor.whiteColor];
    self->floatView.layer.borderColor = [UIColor colorWithWhite:0.6 alpha:0.2].CGColor;
    
    self->floatImage = [[UIImageView alloc] initWithFrame:CGRectMake(23.5, 11.5, 24, 24)];
    [self->floatView addSubview:self->floatImage];
    
    
    self->floatLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 40, 60,20)];
    self->floatLabel.textAlignment = NSTextAlignmentCenter;
    self->floatLabel.textColor = [UIColor colorWithRed:92/255.0 green:185/255.0 blue:66/255.0 alpha:1];
 
    self->floatLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
    self->floatLabel.text = @"";
    [self->floatView addSubview:self->floatLabel];
    
    self->floatBt = [UIButton buttonWithType:UIButtonTypeCustom];
    self->floatBt.frame = CGRectMake(0, 0, 70, 70);
    [self->floatBt addTarget:self action:@selector(floatViewClick) forControlEvents:UIControlEventTouchUpInside];
    [self->floatView addSubview:self->floatBt];
        
    [self addSubview:self->floatView];
}

@end
